//
//  ME_mainView.m
//  ShangChengMoBan
//
//  Created by 张铁军 on 16/8/24.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "ME_mainView.h"
#import "ME_mainViewCell.h"
#import "ME_dingDanBtn.h"

@implementation ME_mainView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        _dataArr = @[@[@{
                           @"image":@"ME_订单",
                           @"name":@"我的订单"
                           }],
                     @[@{
                           @"image":@"ME_话费订单",
                           @"name":@"我的话费订单"
                           },
                       @{
                           @"image":@"ME_一元购",
                           @"name":@"FTP一元购"
                           },
                       @{
                           @"image":@"ME_地址",
                           @"name":@"我的地址"
                           },
                       @{
                           @"image":@"ME_积分",
                           @"name":@"我的积分"
                           },
                       @{
                           @"image":@"ME_收藏",
                           @"name":@"我的收藏"
                           }
                       ]];
        
        
        [self initView];
    }
    return self;
}

-(void)initView{
    
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 200*_kscH)];
    _headView.backgroundColor = [UIColor clearColor];
    
    _backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth,200 *_kscH)];
    _backImageView.image = [UIImage imageNamed:@"ME_我的背景图"];
    _backImageView.userInteractionEnabled = YES;
    [_headView addSubview:_backImageView];
    
    self.sheZhiBtn = [[MYbuttonView alloc] initWithFrame:CGRectMake(KScreenWidth - 50*_kscW, 20*_kscH, 40*_kscW, 40*_kscH)];
    self.sheZhiBtn.tag = 0;
    [self.sheZhiBtn addTarget:self.delegate action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.sheZhiBtn.buttonLabel setHidden:YES];
    self.sheZhiBtn.buttonImaggView.frame = CGRectMake(10*_kscW, 10*_kscH, 18*_kscW, 18*_kscH);
    self.sheZhiBtn.buttonImaggView.image = [UIImage imageNamed:@"ME_设置"];
    [_backImageView addSubview:self.sheZhiBtn];
    
    self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(KScreenWidth/2.0 - 61*_kscW, 67*_kscH, 56*_kscW, 56*_kscH)];
    self.portraitView.image = [UIImage imageNamed:@"未涞商城80"];
    self.portraitView.layer.cornerRadius = 28*_kscW;
    self.portraitView.clipsToBounds = YES;
    [_backImageView addSubview:self.portraitView];
    
    self.nameLab = [[BaseLabel alloc] initWithFrame:CGRectMake(self.portraitView.right + 10*_kscW, self.portraitView.top + 8*_kscH, KScreenWidth-60*_kscW, 20*_kscH)];
    self.nameLab.font = [UIFont fontWithName:Text_Name size:TITLEFONT];
    self.nameLab.textColor = [UIColor whiteColor];
    self.nameLab.textAlignment = NSTextAlignmentLeft;
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    self.nameLab.text = [defaults valueForKey:@"userName"];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(name:) name:@"用户名" object:nil];
    [_backImageView addSubview:self.nameLab];
    
    self.huiYuanZhuangTai = [[BaseLabel alloc] initWithFrame:CGRectMake(self.nameLab.left , self.nameLab.bottom+ 5,self.nameLab.width,self.nameLab.height)];
    self.huiYuanZhuangTai.font = [UIFont fontWithName:Text_Name size:TITLEFONT];
    self.huiYuanZhuangTai.textColor = [UIColor whiteColor];
    self.huiYuanZhuangTai.textAlignment = NSTextAlignmentLeft;
    self.huiYuanZhuangTai.text = @"FTP会员";
    [_backImageView addSubview:self.huiYuanZhuangTai];
    
    
    UIButton *tapBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.portraitView.left - 5*_kscW, self.portraitView.top, self.portraitView.width+10*_kscW,self.portraitView.width+10*_kscW)];
    tapBtn.tag = 1;
    [tapBtn addTarget:self.delegate action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_backImageView addSubview:tapBtn];
    
    _labelBackView = [[UIView alloc]initWithFrame:CGRectMake(0,_backImageView.height - 40*_kscH, KScreenWidth, 40*_kscH)];
    _labelBackView.backgroundColor = [UIColor whiteColor];
    _labelBackView.alpha = 0.45;
    [_backImageView addSubview:_labelBackView];
    
    _labelBackView1 = [[UIView alloc] initWithFrame:_labelBackView.frame];
    
    [_backImageView addSubview:_labelBackView1];
    
//    if (_biaoJi == 0) {
    self.sCJFLAbel = [self returnLabeWithIndex:0 andImage:@"me_main_红包"];
    self.FTPLabel = [self returnLabeWithIndex:1 andImage:@"me_main_余额"];
    self.FTPjFBiLiLabel = [self returnLabeWithIndex:2 andImage:@""];
    //    }else{
//        self.hongBaoLab = [self returnLabeWithIndex:0 andImage:@"me_main_红包"];
//        self.fengLiBiLab = [self returnLabeWithIndex:1 andImage:@"me_main_凤梨币"];
//        self.yuELab = [self returnLabeWithIndex:2 andImage:@"me_main_余额"];
//    }
    
    [self drawDingDanView];

    [self creatTableView];
}
-(void)name:(NSNotification *)not{
    if ([not.object isEqualToString:@"1"]) {
        self.nameLab.text = @"";
    }else{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    self.nameLab.text = [defaults valueForKey:@"userName"];
    }
}
-(void)drawDingDanView{
    _dingDanScorllView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 80 * _kscH)];
    _dingDanScorllView.backgroundColor = [UIColor clearColor];
    UIView *backView = [[UIView alloc]initWithFrame:_dingDanScorllView.frame];
    backView.backgroundColor = [UIColor whiteColor];
    [_dingDanScorllView addSubview:backView];
    
    NSArray *dingDanZTAry = @[@"待付款",@"待发货",@"待收货",@"待评价",@"售后/退货"];
    NSArray *imageAry = @[@"ME_待付款",@"ME_待发货",@"ME_待收货",@"ME_待评价",@"ME_退款售后"];
    
    for (int i = 0; i < 5; i ++) {
        ME_dingDanBtn *btn = [[ME_dingDanBtn alloc]initWithFrame:CGRectMake(64* _kscW * i, 0,64 * _kscW, 80 * _kscH)];
        btn.tapBtn.tag = i;
        [btn.tapBtn addTarget:self.delegate action:@selector(dinaDanBtnTapClicked:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.text = [dingDanZTAry objectAtIndex:i];
        btn.tuBiaoImage.image = [UIImage imageNamed:[imageAry objectAtIndex:i]];
        [_dingDanScorllView addSubview:btn];
    }
}

-(void)creatTableView{
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, self.height) style:UITableViewStyleGrouped];
    _table.delegate = self;
    _table.dataSource = self;
    _table.showsVerticalScrollIndicator = NO;
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:_table];

}

-(BaseLabel *)returnLabeWithIndex:(NSInteger)index andImage:(NSString *)imageName{
    
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(10*_kscW + KScreenWidth/3.0*index, 14*_kscH, 11*_kscW, 10*_kscH)];
    image.image = [UIImage imageNamed:imageName];
    [_labelBackView1 addSubview:image];
    
    BaseLabel *lab = [[BaseLabel alloc] initWithFrame:CGRectMake(image.right +2*_kscW, 13*_kscH, KScreenWidth/3.0 - 24*_kscW, 13*_kscH) andFont:PuTongFont-1];
    lab.textColor = [UIColor whiteColor];
    [_labelBackView1 addSubview:lab];
    
    if (index > 0) {
        UILabel *shuXian = [[UILabel alloc] initWithFrame:CGRectMake(KScreenWidth/3.0 *index, lab.top, 0.7, 15*_kscH)];
        shuXian.backgroundColor = [UIColor whiteColor];
        [_labelBackView1 addSubview:shuXian];
        
    }
    
    return lab;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[_dataArr objectAtIndex:section] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ME_mainViewCell *cell = [[ME_mainViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    if (indexPath.row == 0) {
        cell.top = YES;
    }
    if (indexPath.row == [[_dataArr objectAtIndex:indexPath.section] count]-1) {
        cell.bottom = YES;
    }
    
    NSDictionary *dic = [[_dataArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.signLabel.text = [dic objectForKey:@"name"];
    cell.signImage.image = [UIImage imageNamed:[dic objectForKey:@"image"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return _headView;
    }else{
        return nil;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return _dingDanScorllView;
    }else{
        return nil;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return _headView.height + 10 * _kscH;
    }else{
        return 0.1;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return _dingDanScorllView.height + 10 * _kscH;
    }
    return 10*_kscH;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 36*_kscH;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [[_dataArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    [self.delegate tableClicked:[dic objectForKey:@"name"]];
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
